package com.sunwayworld.basemodule.rm.equipment.equiptborrow.service.impl;

import com.sunwayworld.basemodule.rm.equipment.bean.LimsEquipmentBean;
import com.sunwayworld.basemodule.rm.equipment.equiptborrow.bean.LimsEquiptBorrowBean;
import com.sunwayworld.basemodule.rm.equipment.equiptborrow.dao.LimsEquiptBorrowDao;
import com.sunwayworld.basemodule.rm.equipment.equiptborrow.service.LimsEquiptBorrowService;
import com.sunwayworld.basemodule.rm.equipment.service.LimsEquipmentService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.audit.aunnotation.Audit;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.support.auditable.bean.CoreBpmnParameterDTO;
import com.sunwayworld.framework.utils.CollectionUtils;
import com.sunwayworld.framework.utils.ObjectUtils;
import com.sunwayworld.module.sys.bpmn.bean.CoreBpmnInstanceStatusDTO;
import com.sunwayworld.module.sys.bpmn.diagram.ProcessStatus;
import com.sunwayworld.module.sys.bpmn.engine.CoreBpmnRuntimeService;
import com.sunwayworld.module.sys.bpmn.engine.CoreBpmnRuntimeSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsEquiptBorrowServiceImpl implements LimsEquiptBorrowService {

    @Autowired
    private LimsEquiptBorrowDao limsEquiptBorrowDao;
    @Autowired
    private LimsEquipmentService limsEquiptService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsEquiptBorrowDao getDao() {
        return limsEquiptBorrowDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsEquiptBorrowBean limsEquiptBorrow = jsonWrapper.parseUnique(LimsEquiptBorrowBean.class);
        limsEquiptBorrow.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsEquiptBorrow);
        return limsEquiptBorrow.getId();
    }

    @Transactional
    @AuditTrailEntry(AuditTrailType.PROCESS_COMPLETE)
    @Audit("AUDIT.PROCESS_COMPLETE")
    @Override
    public List<CoreBpmnInstanceStatusDTO<Long>> completeTask(RestJsonWrapperBean wrapper) {
        List<LimsEquiptBorrowBean> itemList = wrapper.parse(this.getDao().getType());
        if (itemList.isEmpty()) {
            return CollectionUtils.emptyList();
        } else {
            this.checkAndUpdateVersion(itemList);
            CoreBpmnRuntimeService runtimeService = (CoreBpmnRuntimeService)ApplicationContextHelper.getBean(CoreBpmnRuntimeService.class);
            List<Long> itemIdList = (List)itemList.stream().map((i) -> {
                return (Serializable)i.getId();
            }).collect(Collectors.toList());
            List<LimsEquiptBorrowBean> selectItemList = this.getBpmnVarsItemList(itemIdList);
            List<CoreBpmnRuntimeSource<LimsEquiptBorrowBean, Long>> runtimeSourceList = this.parseAuditableRuntimeSource(itemIdList, CoreBpmnParameterDTO.of(wrapper), this.getBpmnVars(wrapper, selectItemList));
            List<CoreBpmnInstanceStatusDTO<Long>> coreBpmnInstanceStatusDTOS = runtimeService.completeTask(runtimeSourceList);
            List<CoreBpmnInstanceStatusDTO<Long>> flagList = coreBpmnInstanceStatusDTOS.stream().filter(e -> ObjectUtils.equals(e.getProcessStatus(), ProcessStatus.DONE)).collect(Collectors.toList());
            if (!flagList.isEmpty()) {
                // 流程走完更新设备管理状态
                List<Long> idList = selectItemList.stream().map(equipt -> equipt.getEquiptId()).collect(Collectors.toList());
                List<LimsEquipmentBean> LimsEquipmentBeans = limsEquiptService.selectListByIds(idList);
                if (!LimsEquipmentBeans.isEmpty()) {
                    LimsEquipmentBeans.forEach(equipt->equipt.setEquipState("Borrow"));
                    limsEquiptService.getDao().update(LimsEquipmentBeans,"equipState");
                }
            }
            return coreBpmnInstanceStatusDTOS;
        }
    }
}
